{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "7be17a95",
      "metadata": {},
      "outputs": [],
      "source": [
        "from vllm.benchmarks.datasets import PrefixRepetitionRandomDataset\n",
        "from vllm.transformers_utils.tokenizer import get_tokenizer\n",
        "import daft\n",
        "from daft.functions import monotonically_increasing_id\n",
        "import ray\n",
        "ray.init()\n",
        "daft.set_runner_ray()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "64d8fab9",
      "metadata": {},
      "outputs": [],
      "source": [
        "dataset = PrefixRepetitionRandomDataset(random_seed=42)\n",
        "tokenizer = get_tokenizer(\"Qwen/Qwen3-8B\")\n",
        "root_dir = \"s3://my-bucket/vllm-prefix-caching-partitioned\"\n",
        "\n",
        "def generate_data(request_k: int, num_prefixes: int):\n",
        "    sample = dataset.sample(\n",
        "        tokenizer=tokenizer,\n",
        "        num_requests=request_k * 1000,\n",
        "        prefix_len=256,\n",
        "        suffix_len=256,\n",
        "        num_prefixes=num_prefixes,\n",
        "        output_len=128,\n",
        "    )\n",
        "\n",
        "    sample = [s.prompt for s in sample]\n",
        "    \n",
        "    df = daft.from_pydict(\n",
        "    {\n",
        "        \"prompt\": sample,\n",
        "        }\n",
        "    )\n",
        "\n",
        "    df = df.select(monotonically_increasing_id().alias(\"id\"), \"prompt\")\n",
        "    df = df.repartition(8)\n",
        "    return df.write_parquet(f\"{root_dir}/{request_k}k_0-5_{num_prefixes}.parquet\")\n",
        "\n",
        "\n",
        "def generate_data_no_prefix(request_k: int):\n",
        "    sample = dataset.sample(\n",
        "        tokenizer=tokenizer,\n",
        "        num_requests=request_k * 1000,\n",
        "        prefix_len=0,\n",
        "        suffix_len=512,\n",
        "        output_len=128,\n",
        "    )\n",
        "\n",
        "    sample = [s.prompt for s in sample]\n",
        "    \n",
        "    df = daft.from_pydict(\n",
        "    {\n",
        "        \"prompt\": sample,\n",
        "        }\n",
        "    )\n",
        "\n",
        "    df = df.select(monotonically_increasing_id().alias(\"id\"), \"prompt\")\n",
        "    df = df.repartition(8)\n",
        "    return df.write_parquet(f\"{root_dir}/{request_k}k_0.parquet\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "2727381e",
      "metadata": {},
      "outputs": [],
      "source": [
        "generate_data(200, 8)\n",
        "generate_data(200, 64)\n",
        "generate_data(200, 512)\n",
        "generate_data(200, 4096)\n",
        "generate_data_no_prefix(200)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "11452c07",
      "metadata": {},
      "outputs": [],
      "source": []
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": ".venv",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.13.2"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
